﻿using System;
using System.Linq;
using System.Web.UI.WebControls;

using IdeaNMR.Shared;
using IdeaNMR.Web.Core;
using IdeaNMR.Shared.Utils;

namespace IdeaNMR.Web
{
    public partial class MyDrafts : IdeaNMR_BasePage
    {
        protected class Item
        {
            public BaseObject Obj { get; set; }
        }

        #region Page Lifecycle
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Master.Title = "My Drafts";
        }
        #endregion

        protected void ldsDrafts_OnSelect(object sender, LinqDataSourceSelectEventArgs e)
        {
            IdeaNMRdbContainer inmr = new IdeaNMRdbContainer();
            var drafts1 = from comp in inmr.BaseObjects.OfType<NMRObject>()
                         where comp.IsDraft && comp.Authors.Any(aa => aa.User.Username == ScrewTurnWikiFacade.Username && aa.Index < 0)
                         select new Item
                         {
                             Obj = comp
                         };
            var drafts2 = from comp in inmr.BaseObjects.OfType<Sample>()
                          where comp.IsDraft && comp.User.Username == ScrewTurnWikiFacade.Username
                          select new Item
                          {
                             Obj = comp
                          };
            var drafts3 = from comp in inmr.BaseObjects.OfType<Spectrometer>()
                          where comp.IsDraft && comp.User.Username == ScrewTurnWikiFacade.Username
                          select new Item
                          {
                             Obj = comp
                          };
            e.Result = drafts1.Union(drafts2).Union(drafts3).OrderByDescending(xx => xx.Obj.Datetime);
        }

        protected void butDelete_OnCommand(object sender, CommandEventArgs e)
        {
            Draft.Delete(new Guid(e.CommandArgument.ToString()), ScrewTurnWikiFacade.Username);
            lvDrafts.DataBind();
        }

        #region private Methods
        protected string ShowLink(Guid Id, BaseObject obj)
        {
            UriBuilder uri = new UriBuilder(ScrewTurnWikiFacade.MainUrl);
            string eT = obj.GetEntityType().ToString();

            if (!obj.FirstVersionReference.IsLoaded) obj.FirstVersionReference.Load();
            BaseObject f = null;
            try
            {
                f = (BaseObject)obj.FirstVersion;
            }
            catch (InvalidCastException ex)
            {
                ScrewTurn.Wiki.Log.LogEntry(ex.ToString(), ScrewTurn.Wiki.PluginFramework.EntryType.Error, ScrewTurnWikiFacade.Username);
                Response.Redirect("Error.aspx", true);
            }
            uri.Path += f.IsPrivate
                            ? string.Format("{0}_New.ashx", eT)
                            : string.Format("{0}_id_{1}.ashx", eT, f.Id);
            uri.AddQueryParameter("TargetID", Id.ToString());
            return uri.Uri.ToString();
        }

        protected string ShowType(BaseObject obj)
        {
            return obj.GetEntityType().ToCategoryName();
        }
        #endregion
    }
}